﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Lenovo.CFI.Common
{
    public class ScheduleDetail
    {
        public ScheduleDetail()
        {
        }
        public ScheduleDetail(int id)
        {
            this.id = id;
        }

        public ScheduleDetail(int topicID, ScheduleEventType eventType)
        {
            this.topicID = topicID;
            this.eventType = eventType;
        }

        public ScheduleDetail(int id, int topicID, ScheduleEventType eventType)
        {
            this.id = id;
            this.topicID = topicID;
            this.eventType = eventType;
        }

        private int id;
        private int topicID;
        private ScheduleEventType eventType;
        private Idea idea;
        private int? ideaID;
        private int period;
        private DateTime? until;
        private int sort;

        public int ID
        {
            get { return id; }
            set { id = value; }
        }

        public int TopicID
        {
            get { return topicID; }
            set { topicID = value; }
        }

        /// <summary>
        /// 1 Idea, 2 Break, 3 Start
        /// </summary>
        public ScheduleEventType EventType
        {
            get { return eventType; }
            set { eventType = value; }
        }

        public Idea Idea
        {
            get { return idea; }
            set 
            {
                idea = value; 

                if (value == null)
                    this.ideaID = null;
            }
        }

        public int? IdeaID
        {
            get 
            {
                if (this.idea != null)
                    return this.idea.ID;
                else
                    return ideaID; 
            }
            set 
            { 
                ideaID = value; 
            }
        }

        public int Period
        {
            get { return period; }
            set { period = value; }
        }

        public DateTime? Until
        {
            get { return until; }
            set { until = value; }
        }

        public int Sort
        {
            get { return sort; }
            set { sort = value; }
        }


        private DateTime startTime;
        private DateTime endTime;

        /// <summary>
        /// 记录每个Idea评审开始时间(推算得出，数据库不存储)
        /// </summary>
        public DateTime StartTime
        {
            get { return startTime; }
            //set { startTime = value; }
        }

        /// <summary>
        /// 记录每个Idea评审结束时间(推算得出，数据库不存储)
        /// </summary>
        public DateTime EndTime
        {
            get { return endTime; }
            //set { endTime = value; }

            //get
            //{
            //    DateTime endTime;
            //    switch (eventType)
            //    {
            //        case ScheduleEventType.Idea:
            //            endTime = startTime.AddMinutes(period);
            //            break;
            //        case ScheduleEventType.Start:
            //            endTime = until.Value;
            //            break;
            //        case ScheduleEventType.Break:
            //            endTime = (until != null ? until.Value : startTime.AddMinutes(period));
            //            break;
            //        default:
            //            endTime = startTime.AddMinutes(period);
            //            break;

            //    }
            //    return endTime;
            //}
        }

        public void SetTime(DateTime? start)
        {
            if (this.eventType == ScheduleEventType.Start)
            {
                this.startTime = until.Value;
                this.endTime = until.Value;
            }
            else if (this.eventType == ScheduleEventType.Idea)
            {
                this.startTime = start.Value;
                this.endTime = startTime.AddMinutes(period);
            }
            else if (this.eventType == ScheduleEventType.Break)
            {
                if (until.HasValue)
                {
                    this.startTime = start.Value;
                    this.endTime = until.Value;
                    this.period = (int)(this.endTime - this.startTime).TotalMinutes;        // 修正
                }
                else
                {
                    this.startTime = start.Value;
                    this.endTime = startTime.AddMinutes(period);
                }
            }
        }
    }

    public enum ScheduleEventType
    {
        /// <summary>
        /// Idea
        /// </summary>
        Idea = 1,

        /// <summary>
        /// 休息时间段
        /// </summary>
        Break = 2,

        /// <summary>
        /// 开始时间点
        /// </summary>
        Start = 3
    }
}
